      1  import java.util.Arrays;

      2  import java.util.Scanner;

      3

      4  /**

      5     This program demonstrates the binary search algorithm.

      6  */

      7  public class BinarySearchDemo

      8  {

      9     public static void main(String[] args)

     10     {

     11        // Construct random array

     12

     13        int[] a = ArrayUtil.randomIntArray(20, 100);

     14        Arrays.sort(a);

     15        System.out.println(Arrays.toString(a));

     16        Scanner in = new Scanner(System.in);

     17

     18        boolean done = false;

     19        while (!done)

     20        {

     21           System.out.print

     22                 ("Enter number to search for, -1 to quit: ");

     23           int n = in.nextInt();

     24           if (n == -1)

     25           {

     26              done = true;

     27           }

     28           else

     29           {

     30              int pos = BinarySearcher.search(a, 0, a.length - 1, n);

     31              System.out.println("Found in position " + pos);

     32           }

     33        }

     34     }

     35  }